do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*prize"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	
drop if prize10M > 5	
gen married_presingle = married if married_pre1 == 0
gen divorced_premarried = married == 0 if married_pre1 == 1

replace norm_year = norm_year + 3

cap rm "$table/temp/AppG.txt"
cap rm "$table/temp/AppG.xls"
	
foreach y in married_presingle divorced_premarried{
		
	reghdfe totbaby currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if `y' != ., a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(All,`y',X->Y)
	reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(All,`y',X->M)
	reghdfe totbaby `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-M.xls" , append bd(3) sd(3) nocon keep(`y' currentxprize*) ctitle(All,`y',M->Y)

}

cap rm "$table/temp/AppG-Male.txt"
cap rm "$table/temp/AppG-Male.xls"
	
foreach y in married_presingle divorced_premarried{
		
	reghdfe totbaby currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if `y' != . & pers_gender == 1, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-Male.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(Male,`y',X->Y)
	reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if pers_gender == 1, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-Male.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(Male,`y',X->M)
	reghdfe totbaby `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if pers_gender == 1, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-Male.xls" , append bd(3) sd(3) nocon keep(`y' currentxprize*) ctitle(Male,`y',M->Y)

}

cap rm "$table/temp/AppG-Female.txt"
cap rm "$table/temp/AppG-Female.xls"
	
foreach y in married_presingle divorced_premarried{
	reghdfe totbaby currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if `y' != . & pers_gender == 2, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-Female.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(Female,`y',X->Y)
	reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if pers_gender == 2, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-Female.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(Female,`y',X->M)
	reghdfe totbaby `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if pers_gender == 2, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/AppG-Female.xls" , append bd(3) sd(3) nocon keep(`y' currentxprize*) ctitle(Female,`y',M->Y)

}

foreach x in "" "-Male" "-Female"{
	import delimited using "$table/temp/AppG`x'.txt", clear
	
	replace v4  = v4[_n+20]  in 6/7 
	replace v7 = v7[_n+22] in 6/7 
	replace v1 = "M" in 6 // Mediation Variable

	drop in 26/34

	gen row = _n
	replace row = row + 2 if row > 25
	set obs 32
	replace row = 26.5 if row == .
	sort row
	drop row

	replace v1 = "Product of Coefficients" in 27
	replace v1 = "Difference in Coefficients" in 28

	forv i = 1(1)2{
		loc j = `i'*3-1
		loc k = `i'*3
		loc l = `i'*3+1
		loc a = subinstr(v`j'[24],"*","",.) // X -> Y
		loc a: dis `a'
		loc a: dis `a'
		loc b = subinstr(v`k'[24],"*","",.) // X -> M
		loc b: dis `b'
		loc b: dis `b'
		loc c = subinstr(v`l'[6],"*","",.) // M -> Y | X
		loc c: dis `c'
		loc c: dis `c'
		loc a2 = subinstr(v`l'[24],"*","",.) // X -> Y | M
		loc a2: dis `a2'
		loc a2: dis `a2'
		loc m_poc = `b'*`c'
		loc m_dic = `a'-`a2'
		loc p_poc = `m_poc' / `a' * 100
		loc p_dic = `m_dic' / `a' * 100
		loc m_poc: dis %4.3f `m_poc'
		loc m_dic: dis %4.3f `m_dic'
		loc p_poc: dis %4.2f `p_poc'
		loc p_dic: dis %4.2f `p_dic'
		loc p_poc = "`p_poc'"+"%"
		loc p_dic = "`p_dic'"+"%"
		
		replace v`k' = "`m_poc'" in 27
		replace v`k' = "`m_dic'" in 28
		replace v`l' = "`p_poc'" in 27
		replace v`l' = "`p_dic'" in 28
	}

	save "$table/temp/AppG`x'.dta", replace
}

clear
foreach x in "" "-Male" "-Female"{
	ap using "$table/temp/AppG`x'.dta"
}

export excel "$table/Tables.xlsx", sheet("AppG") sheetreplace 
